﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.IO;


namespace HRdex_Final.Profile
{
    public partial class MyAccount : System.Web.UI.Page
    {
        string SessionID()
        {
            return Session["EmployeeID"].ToString();
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            lblConfirmationMessage.Visible = false;
            if (!Page.IsPostBack)
            {
                GetData();
                string connectionString = "Data Source=.\\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\HRdexDB.mdf;Integrated Security=True;User Instance=True";
                SqlConnection Conn = new SqlConnection(connectionString);

                SqlCommand cm = new SqlCommand("select * from formEmployees WHERE (EmployeeID="+SessionID()+")", Conn);
                // SqlCommand Comm1 = new SqlCommand(cm, Conn);
                Conn.Open();
                SqlDataReader DR1 = cm.ExecuteReader();
                if (DR1.Read())
                {
                    lblName.Text = DR1.GetValue(1).ToString() + "," + DR1.GetValue(2).ToString() + " " + DR1.GetValue(3).ToString();
                    txtUName.Text = DR1.GetValue(0).ToString();
                    txtName.Text = DR1.GetValue(1).ToString() + "," + DR1.GetValue(2).ToString() + " " + DR1.GetValue(3).ToString();
                    txtemail.Text = DR1.GetValue(11).ToString();
                    txtnum.Text = DR1.GetValue(10).ToString();
                }
                Conn.Close();
            }
        }

        private DataSet GetData()
        {
            string Uname = txtUName.Text;
            string pw = txtpw.Text;
            string pwv = txtpwV.Text;
            string name = txtName.Text;
            string eadd = txtemail.Text;
            string num = txtnum.Text;

            string connectionString = "Data Source=.\\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\HRdexDB.mdf;Integrated Security=True;User Instance=True";
            SqlConnection myConnect = new SqlConnection(connectionString);
            SqlDataAdapter ad = new SqlDataAdapter("SELECT * FROM formEmployees", myConnect);

            DataSet ds = new DataSet();
            ad.Fill(ds);
            return ds;

        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            //string pw = txtpwV.Text;
            //string contact = txtnum.Text;
            //string eadd = txtemail.Text;

            //int n;
            //n = Convert.ToInt32(txtnum.Text);
            //n = int.Parse(txtnum.Text);

            string connString = "Data Source=.\\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\HRdexDB.mdf;Integrated Security=True;User Instance=True";
            SqlConnection conn = new SqlConnection(connString);
            conn.Open();
            SqlCommand cmd = new SqlCommand("UPDATE formEmployees SET Password = @Password, ContactNo = @ContactNo, Email = @Email WHERE (EmployeeID=" + SessionID() + ")", conn);
            cmd.Parameters.AddWithValue("@Password", txtpwV.Text);
            cmd.Parameters.AddWithValue("@ContactNo", txtnum.Text);
            cmd.Parameters.AddWithValue("@Email", txtemail.Text);

            cmd.ExecuteNonQuery();
            conn.Close();
            lblConfirmationMessage.Visible = true;
            lblConfirmationMessage.Text = "You successfully update your profile account.";
        }

        void Select()
        {
            string FlexiID = Request.QueryString["FlexiID"];

        }
    }
}